//
//  MPTabBarController.m
//  SearchPeople
//
//  Created by YoloMao on 16/5/5.
//  Copyright © 2016年 zk. All rights reserved.
//

#import "MPTabBarController.h"
#import "MPDefine.h"

@interface MPTabBarController ()

@end

@implementation MPTabBarController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self MPsetTabBarViewController];//这里先后顺序很重要
    [self setTabBarItems];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

//设置自己的tabbaritem
- (void)setTabBarItems
{
    self.tabBar.shadowImage = [UIImage new];
//    self.tabBar.backgroundImage = [UIImage new];
    self.tabBar.translucent = NO;
    self.tabBar.opaque = YES;
    [[UITabBar appearance] setBarTintColor:CustomTintColor];
    
#warning 2种方法 被注释掉的这种会有点问题在navBar上
    //https://forums.xamarin.com/discussion/25685/ios-bartintcolor-is-always-lighter-than-the-color-that-is-defined
    [[UINavigationBar appearance] setTranslucent:NO];
    [[UINavigationBar appearance] setOpaque:YES];
    [[UINavigationBar appearance] setBarTintColor:CustomTintColor];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setTitleTextAttributes:
     @{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:[[UIImage imageNamed:@"NavBackButton"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    
    UITabBarItem * FirstTabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页"
                                                                   image:[UIImage imageNamed:@"tabItem_home@2x"]
                                                           selectedImage:[UIImage imageNamed:@"tabItem_home@2x"]];
    
    UITabBarItem * SecondTabBarItem = [[UITabBarItem alloc] initWithTitle:@"搜索"
                                                                    image:[UIImage imageNamed:@"tabItem_search@2x"]
                                                            selectedImage:[UIImage imageNamed:@"tabItem_search@2x"]];
    
    UITabBarItem * ThirdTabBarItem = [[UITabBarItem alloc] initWithTitle:@"收藏夹"
                                                                   image:[UIImage imageNamed:@"tabItem_fav@2x"]
                                                           selectedImage:[UIImage imageNamed:@"tabItem_fav@2x"]];
    
    UITabBarItem * FourTabBarItem = [[UITabBarItem alloc]initWithTitle:@"个人中心"
                                                                 image:[UIImage imageNamed:@"tabItem_mine@2x"]
                                                         selectedImage:[UIImage imageNamed:@"tabItem_mine@2x"]];
    
    
    NSArray * itemArray = @[FirstTabBarItem,SecondTabBarItem,ThirdTabBarItem,FourTabBarItem];
    
    for (NSInteger i = 1; i <= itemArray.count ; i ++)
    {
        UIViewController *VC = self.viewControllers[i-1];
        
        UITabBarItem *item = [itemArray objectAtIndex:(i-1)];
        [item setTag:i];
        VC.tabBarItem = item;
    }
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:select_Color_TabItem, NSForegroundColorAttributeName,[UIFont systemFontOfSize:13],NSFontAttributeName, nil] forState:UIControlStateSelected];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:normal_Color_TabItem, NSForegroundColorAttributeName,[UIFont systemFontOfSize:13],NSFontAttributeName, nil] forState:UIControlStateNormal];
    
    
    for(UITabBarItem *item in self.tabBar.items)
    {
        item.selectedImage = [item.selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        item.image = [item.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
}

- (void)MPsetTabBarViewController
{
    UIStoryboard *homePage = [UIStoryboard storyboardWithName:@"HomePage" bundle:nil];
    UIViewController *controller1 = [homePage instantiateInitialViewController];
    UIStoryboard *search = [UIStoryboard storyboardWithName:@"Search" bundle:nil];
    UIViewController *controller2 = [search instantiateInitialViewController];
    UIStoryboard *fav = [UIStoryboard storyboardWithName:@"Favorites" bundle:nil];
    UIViewController *controller3 = [fav instantiateInitialViewController];
    UIStoryboard *mine = [UIStoryboard storyboardWithName:@"Mine" bundle:nil];
    UIViewController *controller4 = [mine instantiateInitialViewController];
    self.viewControllers = [NSArray arrayWithObjects:controller1, controller2,controller3,controller4, nil];
}

@end
